<template>
	<el-container v-loading="loading">
		<el-header style="height: auto;padding: 10px 10px;">
			<div class="left-panel">
				<el-date-picker style="width: 180px;margin-right: 10px;" v-model="budgetYear" @change="searchHandler" format="YYYY" value-format="YYYY" type="year" placeholder="请选择预算年份"/>
				<fsButton ref="button" plain size="small" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
			<div class="right-panel">
				<ex-search ref="search" :searches="searches" @searchOk="searchHandler" :initParams="{}"></ex-search>
			</div>
		</el-header>
		<el-main class="nopadding reportSummaryTable" style="padding: 0 5px;">
			<el-table id="srtatusTable" :data="assetData" border style="width: 100%">
				<template v-for="column in this.columns" :key="column.code">
					<el-table-column :prop="column.code"
									 :label="column.name"
									 :formatter="this.formatters"
									 :align="'center'"
									 :sortable="column.sortable?column.sortable:false"
									 :column-key="column.code"
									 :show-overflow-tooltip="true"
									 :width="column.width?column.width:null">
					</el-table-column>
				</template>
			</el-table>
		</el-main>
	</el-container>
	<data-card ref="dataCard"></data-card>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import scSelectFilterLink from "@/components/scSelectFilterLink/index.vue";
import DataCard from "@/views/asset/card/card.vue";
import print from "@/utils/print";
import tool from "@/utils/tool";
import {h} from "vue";
import {ElButton, ElContainer} from "element-plus";
import globalparam from "@/constant/model/globalparam";
import moment from "moment";
export default {
	name: '预算统计',
	components: {
		DataCard,
		scSelectFilterLink,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
	},
	data() {
		return {
			elk:0,
			dialog: {
				save: false,
				permission: false,
			},
			apiObj: [],
			buttons: [
				{name: "导出", icon: "el-icon-download", click: "export",  type: "info", permission: "export"},
				{name: "打印", icon: "el-icon-download", click: "printTable", type: "info", permission: "print"},
			],
			searches: [
				{type: "select", name: "预算名称", code: "budgetIds", placeholder: "请选择预算名称", dictCode:'FamsBudget',multiple:true},
				{type: "tree-select", name: "预算部门", code: "deptIds", placeholder: "请选择预算部门", dictCode:'SysOrganization',checkStrictly:true,multiple:true,dataPermissionProcess:true},
				{type: "tree-select", name: "资产类别", code: "assetCategoryIds", placeholder: "请选择资产类别", dictCode:'FamsAssetCategory',checkStrictly:true,multiple:true,dataPermissionProcess:true},
				// {type: "date", name: "预算年份", code: "year", placeholder: "请选择预算年份", formatter:"YYYY",valueFormat:"YYYY" },
			],
			columns: [
				{code: "code", name: "预算编号", width:"100"},
				{code: "name", name: "预算名称", width:"200"},
				{code: "purchaseCount", name: "采购数量",click:"show",width:"200"},
				{code: "purchaseAmount", name: "采购金额",click:"show",width:"200"},
				{code: "amount", name: "预算金额",width:"200"},
				{code: "surplusAmount", name: "剩余金额",width:"200"},
				{code: "dateRange", name: "日期范围",width:"200"},
				{code: "deptNames", name: "预算部门",width:"200"},
				{code: "assetCategoryNames", name: "资产类别",width:""},
			],
			parentLevelCode:"",
			assetIds:[],
			assetData: [],
			budgetYear: '',
			loading: false,
			loginUsers: this.$TOOL.current.user(),
		}
	},
	created() {
	},
	mounted() {
		this.searchHandler();
	},
	beforeMount() {
	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		async searchHandler() {
			this.loading = true;
			//释放搜索组件的loading状态
			if (this.$refs.search) {
				this.$refs.search.setLoading(true);
			}
			this.$refs.search.drawer=false;
			let queryParams = {}
			Object.assign(queryParams, this.$refs.search.searchParams());
			queryParams.year = this.budgetYear;
			this.$API.system.budget.budgetSummary.post(queryParams).then((rsp) =>{
				if(rsp.code === 2000 && rsp.data){
					this.assetData = rsp.data;
				}
				this.loading = false;
				//释放搜索组件的loading状态
				if (this.$refs.search) {
					this.$refs.search.setLoading(false);
				}
			});
		},
		//导出
		async export(_this,index) {
			_this.$refs.button.loading(index, true);
			let queryParams = {}
			Object.assign(queryParams, _this.$refs.search.searchParams());
			queryParams.year = _this.budgetYear;
			queryParams.exportType = "budgetSummary";
			let res = await _this.$API.system.budget.export.post(queryParams);
			if (res.code === 2000) {
				await _this.$alert(_this.$t('common.export_request_success'), "提示", {type: 'success'}).catch(()=>{});
			}
			_this.$refs.button.loading(index, false);
		},
		printTable(){
			let printContent = document.getElementById("srtatusTable");
			print(printContent);
		},
		show(_this,row){
			let queryParams = {}
			Object.assign(queryParams, _this.$refs.search.searchParams());
			queryParams.year = _this.budgetYear;
			if(queryParams.deptIds){
				queryParams.usingDepartmentIds = queryParams.deptIds;
				queryParams.deptIds = [];
			}else{
				queryParams.usingDepartmentIds = row.deptIds;
			}
			if(!queryParams.assetCategoryIds){
				queryParams.assetCategoryIds = row.assetCategoryIds;
			}
			queryParams.boughtDateStart = row.year+"0101";
			if(moment(new Date()).year() == row.year){
				queryParams.boughtDateEnd = moment(new Date()).format("YYYYMMDD")
			}else{
				queryParams.boughtDateEnd = row.year+"1231";
			}
			_this.searchAsset(row.name, queryParams);
		},
		searchAsset(item,queryParams) {
			let title = "预算统计"+item+"资产明细";
			//跳转到搜索台账页面
			let fromRouteId = this.$TOOL.crypto.MD5(this.$route.path);//当前页面的路由地址
			let key = this.$TOOL.crypto.MD5(item);//可以做为key的关键字段或内容
			/**
			 *  1 = 根据管理部门，根据登录账户管理范围，资产的管理部门在账户管理范围内的显示<br>
			 *  2 = 根据使用部门，根据登录账户管理范围，资产的使用部门在账户管理范围内的显示<br>
			 *  3 = 根据管理部门或使用部门，根据登录账户管理范围，资产的使用部门或管理部门在账户管理范围内的显示<br>
			 *  其它 = 根据管理部门，按台账，根据使用部门或管理部门或借用部门在管理范围内的
			 */
				//设置搜索参数
			let routeParams = {
					id: key,
					title: title,
					fixedParams:{
						assetStatisticsRange:queryParams.assetStatisticsRange,
						deptIds:queryParams.deptIds,
						assetCategoryIds: queryParams.assetCategoryIds,
					},//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
					searchParams: queryParams
				};
			this.$store.dispatch("addRouteParams", routeParams);
			this.$router.push({
				name: "toAssetList",
				params: {fromRouteId: fromRouteId, key: key}
			});
		},
		formatters(row, column, cellValue, index) {
			for (const item of this.columns) {
				if (item.code === column.property){
					if (item.click) {
						if (item.fixedValue) {
							cellValue = item.fixedValue;
						} else {
							if (item.formatter) {
								if (item.formatter instanceof Function) {
									cellValue = item.formatter(row, cellValue, this);
								} else {
									cellValue = tool.format.convert(cellValue, item.formatter, item.key);
								}
							}
						}
						let label = cellValue+'';
						let clicks = item.click.split(',');
						let labels = label.split(',');
						let permissions = [];
						if (item.permissions) {
							permissions = item.permissions.split(',');
							// permissions.splice(permissions.indexOf(''), 1);
						}
						let children = [];
						if(index < (this.assetData.length - 1) ){
							clicks.forEach((click, index) => {
								let elBtn = h(ElButton, {
									link: true,
									type: 'primary',
									style: {margin: 'auto'},
									size: 'small',
									["onClick"]: () => {
										this.$options.methods[click](this, row);
										// this.$emit('columnClick', click, row);
									}
								}, () => labels[index])
								if (permissions.length > 0) {
									if (permissions.filter(item => item === click).length > 0) {
										children.push(elBtn);
									}
								} else {
									children.push(elBtn);
								}
							});
						}else{
							return cellValue;
						}
						// cellValue = h(ElButton,{link:true,type:'primary',size:'small',["onClick"]:()=>{ this.$emit('columnClick',item.click,row);}},()=>label);
						cellValue = h(ElContainer, {key: tool.uuid("elk"), style: {display: 'flex'}}, () => children);
					}else{
						if (item.formatter) {
							if (item.formatter instanceof Function) {
								cellValue = item.formatter(row, cellValue, this, index);
							} else {
								cellValue = tool.format.convert(cellValue, item.formatter, item.key);
							}
						}
					}
				}

			}
			return cellValue;
		},
	}
}

</script>
<style>



</style>
